package com.chenexample.myshop16173.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chenexample.myshop16173.Exchanfe;
import com.chenexample.myshop16173.R;
import com.chenexample.myshop16173.shuju;
import com.chenexample.myshop16173.util.SPUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class MyRVAdapter1 extends RecyclerView.Adapter<MyRVAdapter1.MyTVHolder> {

    private final LayoutInflater mLayoutInflater;
    private final Context mContext;
    List<Exchanfe> mData;

    public MyRVAdapter1(Context context, List<Exchanfe> data) {
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;
        mData = data;

    }

    @Override
    public MyRVAdapter1.MyTVHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyRVAdapter1.MyTVHolder(mLayoutInflater.inflate(R.layout.activity_exchange_item, parent, false));
    }


    @Override
    public void onBindViewHolder(final MyRVAdapter1.MyTVHolder holder, final int pos) {
        Log.e("aa", mData + "");
        holder.tv11.setText(mData.get(pos).getGood_name());
        holder.tv22.setText(mData.get(pos).getGood_price() + "0积分");
        Glide.with(mContext)
                .load(mData.get(pos).getImage())
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(60)))//圆角半径
                .into(holder.imageView);
        holder.cd1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new QMUIDialog.MessageDialogBuilder(mContext)
                        .setTitle("兑换")
                        .setMessage("确定对换" + mData.get(pos).getGood_name() + "吗?")
                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();

                            }
                        })
                        .addAction("确定", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                int a = (int) SPUtils.get(mContext, "jifen", 0);
                                String a1 = mData.get(pos).getGood_price();
                                int b = Integer.parseInt(a1) * 10;
                                int  s= (int) SPUtils.get(mContext, "jifen", 0);
                                if (a >= b) {
                                    SPUtils.get(mContext, "jifen", a - b);
                                    Toast.makeText(mContext,"兑换成功，剩余积分"+s,Toast.LENGTH_LONG).show();
                                    dialog.dismiss();
                                } else {
                                    Toast.makeText(mContext,"积分不足，当前积分"+s+"需要积分"+b,Toast.LENGTH_LONG).show();
                                    dialog.dismiss();
                                }
                            }
                        })
                        .show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    class MyTVHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView tv11, tv22;
        CardView cd1;

        MyTVHolder(View itemView) {
            super(itemView);
            tv11 = itemView.findViewById(R.id.tv11);
            tv22 = itemView.findViewById(R.id.tv22);
            imageView = itemView.findViewById(R.id.image1);
            cd1 = itemView.findViewById(R.id.cd1);
        }
    }
}